package cn.zzp.system.controller;

import cn.zzp.basic.util.JsonResult;
import cn.zzp.basic.util.PageList;
import cn.zzp.system.domain.Menu;
import cn.zzp.system.query.MenuQuery;
import cn.zzp.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName MenuController
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/17 10:24
 */
@Api(tags = "菜单模块")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public List<Menu> queryAll() {
        return menuService.queryAll();
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name="id", dataType="Long", required=true, value="用户Id")
    })
    @ApiOperation(value = "根据id查询一条数据" )
    @GetMapping("/{id}")
    public Menu queryOne(@PathVariable("id") Long id) {
        return menuService.queryById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @ApiOperation(value = "删除操作" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            menuService.delete(id);
            return  JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 添加或修改
     * @param menu
     * @return
     */
    @ApiOperation(value = "添加或修改" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody Menu menu){
        try {
            if(menu.getId() == null){
                menuService.add(menu);
            }else{
                menuService.update(menu);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页高级查询" )
    @PostMapping
    public PageList<Menu> queryPage(@RequestBody MenuQuery query){
        try {
            return menuService.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            menuService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false);
        }
    }

    @GetMapping("/menuTree")
    public List<Menu> menuTree(){
        return menuService.menuTree();
    }
}
